<script lang="ts" setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app'
import { addQuestionnaire, getQuestionnaireList, getTrainingCourseById } from "../../api/training";

const questionnaire = ref([])
const id = ref('')
const startTime = ref('')
const rateValue = ref(0)
const showSubmitButton = ref(true)

onLoad((options) => {
  id.value = options.id
  startTime.value = options.startTime || ''

  // 检查startTime是否小于当前时间（只比较日期部分）
  if (startTime.value) {
    const startDate = new Date(startTime.value)
    startDate.setHours(0, 0, 0, 0)
    const now = new Date()
    now.setHours(0, 0, 0, 0)
    console.log("课程开始时间：startDate:" + startDate)
    console.log("系统时间：now:" + now)
    showSubmitButton.value = startDate <= now

    console.log("showSubmitButton:" + showSubmitButton.value)
  }

  getData()
})

async function getData() {
  uni.showLoading()
  const ret = await getQuestionnaireList(id.value as string)
  uni.hideLoading()
  questionnaire.value = ret.data
}


async function submit() {

  const params = questionnaire.value.map(it => {
    return {
      trainingCourseId: it.trainingCourseId,
      teacherId: uni.getStorageSync("userInfo").id,
      questionnaireId: it.id,
      questionnaireContent: it.questionnaireContent
    }
  })
  uni.showLoading()
  const ret = await addQuestionnaire(params)
  uni.showToast({
    title: ret.code !== 200 ? '提交失败' : '提交成功',
    icon: 'noe',
    duration: 1500
  })
  if (ret.code !== 200) return
  uni.navigateBack()
}
</script>

<template>
  <view class="T_page">
    <view class="wrap">
      <view>
        <view class="tips">
          <view>调查说明</view>
          <text>亲爱的学员，感谢您参与本次课程！为持续提升培训质量，请您花费几分钟时间完成以下反馈，您的建议对我们非常重要。 </text>
        </view>
      </view>
      <view v-for="(item, index) in questionnaire" :key="item.id">
        <view class="rate" v-if="item.questionnaireType == 0">
          <view class="required">{{ index + 1 }}.{{ item.questionnaireTitle }}</view>
          <uni-rate v-model="item.questionnaireContent" activeColor="#3B82F6" />
        </view>
        <view class="input" v-if="item.questionnaireType == 1">
          <view>{{ index + 1 }}.{{ item.questionnaireTitle }}</view>
          <uni-easyinput type="textarea" placeholder="请输入您的建议..." :maxlength="200" :rows="1" :autoHeight="true"
            v-model="item.questionnaireContent" :border="false" />
        </view>
      </view>
    </view>
    <button class="submit" type="primary" @click="submit" v-if="showSubmitButton">提交问卷</button>
  </view>
</template>

<style lang="scss" scoped>
.T_page {
  display: flex;
  flex-direction: column;
  padding: 0 0 30rpx;
}

.wrap {
  display: flex;
  flex-direction: column;
  gap: 30rpx;
  overflow-y: auto;
  padding: 0 30rpx 30rpx;

  >view {
    >view {
      background-color: #fff;
      border-radius: 20rpx;
      padding: 30rpx;
    }

    >.tips {
      >view {
        color: #000;
      }

      >text {
        margin-top: 14rpx;
        display: block;
        font-size: 24rpx;
        line-height: 40rpx;
        color: #4B5563;
      }
    }

    >.rate,
    >.input {
      ::v-deep {
        .uni-easyinput {
          height: 260rpx;

          .uni-easyinput__content {
            height: 100%;

            textarea {
              font-size: 24rpx;
              height: 100% !important;
            }
          }
        }
      }

      >view {
        color: #000;
        margin-bottom: 30rpx;

        &.required::before {
          content: '* ';
          color: #EF4444;
        }
      }
    }
  }
}

.submit {
  flex-shrink: 0;
  margin: 0 30rpx auto;
  width: calc(100% - 60rpx);
}
</style>
